package com.woldier.filter;


import com.alibaba.fastjson.JSON;
import com.woldier.common.BaseContext;
import com.woldier.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.security.auth.login.CredentialException;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 用于检查是否登录 未登录条状到登陆页面
 */
@WebFilter(filterName = "loginCheckFilter",urlPatterns = "/*" )
@Slf4j
public class LoginCheckFilter implements Filter {
    /**
     * 排除过滤的网址
     */
    private final static String[] urls= {

            "/backend/**" ,//所有静态资源放行
            "/front/**",
            "/employee/login",
            "/employee/logout",
            "/user/login",
            "/user/sendMsg"
    };


    /**
     * 用于进行路径匹配 支持模糊路径
     */
    private final static AntPathMatcher pathMatcher = new AntPathMatcher();




    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String uri = request.getRequestURI();
//        log.info("拦截到路径 {} 请求",uri);
        /*判断是否为放行路径*/
        boolean b = checkURI(uri);
        if (b == true ){
            /*是放行路径*/
//            log.info("放行{} 请求",uri);
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }

        /*不是静态资源路径或者登录路径 则判断是否登录*/
        if(request.getSession().getAttribute("employee")!=null) {
//            log.info("用户已登录,{}",uri);
            /*将用户id放入线程共享空间  用于 MyMetaObjectHandler.java 中通过线程获取对应的用户id 进行通用字段的填充*/
            BaseContext.setCurrentId((Long) request.getSession().getAttribute("employee"));
            /*已经登录*/
            filterChain.doFilter(servletRequest, servletResponse);

            /*不执行后面的语句*/
            return;
        }

//        log.info("用户未登录,{}",uri);


        /*不是静态资源路径或者登录路径 则判断是否登录*/
        if(request.getSession().getAttribute("user")!=null) {
//            log.info("用户已登录,{}",uri);
            /*将用户id放入线程共享空间  用于 MyMetaObjectHandler.java 中通过线程获取对应的用户id 进行通用字段的填充*/
            BaseContext.setCurrentId((Long) request.getSession().getAttribute("user"));
            /*已经登录*/
            filterChain.doFilter(servletRequest, servletResponse);

            /*不执行后面的语句*/
            return;
        }

        /*未登录 返回信息*/
        /*这里返回code=0,msg=NOTLOGIN 前端收到对应的数据就会跳转*/
        response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
        return;

    }

    /**
     * 校验uri
     *
     * */

    private boolean checkURI(String URI){
        for (String url : urls) {
            boolean b = pathMatcher.match(url, URI);
            if(b == true)
                return true;
        }
        return false;
    }
}
